package xiaoqi.springboot.boot.rpc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import xiaoqi.springboot.boot.rpc.api.UserRpcService;
import xiaoqi.springboot.boot.rpc.dto.UserAddDTO;
import xiaoqi.springboot.boot.rpc.dto.UserDTO;

import javax.annotation.Resource;


@SpringBootApplication
@ImportResource("classpath:dubbo.xml")
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

    @ComponentScan
    public class UserRpcTest implements CommandLineRunner {

        private final Logger logger = LoggerFactory.getLogger(getClass());

        @Resource
        private UserRpcService userRpcService;

        @Override
        public void run(String... args) throws Exception {
            UserDTO user = userRpcService.get(1);
            logger.info("[run][发起一次 Dubbo RPC 请求, 获得用户为({})]", user);
        }
    }

    @ComponentScan
    public class UserRpcServiceTest02 implements CommandLineRunner {

        private final Logger logger = LoggerFactory.getLogger(getClass());

        @Resource
        private UserRpcService userRpcService;

        @Override
        public void run(String... args) throws Exception {
            // 获得用户
            try {
                UserDTO user = userRpcService.get(null);
                logger.info("[run][发起一次 Dubbo RPC 请求， 获得用户为({})]", user);
            } catch (Exception e) {
                logger.error("[run][获得用户发生异常, 信息为:[{}]", e.getMessage());
            }

            // 添加用户
            try {
                UserAddDTO addDTO = new UserAddDTO();
                addDTO.setName("chengxqqqqq");
                addDTO.setGender(null);

                userRpcService.add(addDTO);
                logger.info("[run][发起一次 Dubbo RPC 请求, 添加用户为({})]", addDTO);

            } catch (Exception e) {
                logger.error("[run][添加用户发生异常,信息为:[{}]]", e.getMessage());
            }
        }
    }
}
